// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <GaussianControlPointList.h>
#include <DataNode.h>
#include <GaussianControlPoint.h>

// ****************************************************************************
// Method: GaussianControlPointList::GaussianControlPointList
//
// Purpose:
//   Init utility for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void GaussianControlPointList::Init()
{

    GaussianControlPointList::SelectAll();
}

// ****************************************************************************
// Method: GaussianControlPointList::GaussianControlPointList
//
// Purpose:
//   Copy utility for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void GaussianControlPointList::Copy(const GaussianControlPointList &obj)
{
    AttributeGroupVector::const_iterator pos;

    // *** Copy the controlPoints field ***
    // Delete the AttributeGroup objects and clear the vector.
    for(pos = controlPoints.begin(); pos != controlPoints.end(); ++pos)
        delete *pos;
    controlPoints.clear();
    if(obj.controlPoints.size() > 0)
        controlPoints.reserve(obj.controlPoints.size());
    // Duplicate the controlPoints from obj.
    for(pos = obj.controlPoints.begin(); pos != obj.controlPoints.end(); ++pos)
    {
        GaussianControlPoint *oldGaussianControlPoint = (GaussianControlPoint *)(*pos);
        GaussianControlPoint *newGaussianControlPoint = new GaussianControlPoint(*oldGaussianControlPoint);
        controlPoints.push_back(newGaussianControlPoint);
    }


    GaussianControlPointList::SelectAll();
}

// Type map format string
const char *GaussianControlPointList::TypeMapFormatString = GAUSSIANCONTROLPOINTLIST_TMFS;
const AttributeGroup::private_tmfs_t GaussianControlPointList::TmfsStruct = {GAUSSIANCONTROLPOINTLIST_TMFS};


// ****************************************************************************
// Method: GaussianControlPointList::GaussianControlPointList
//
// Purpose:
//   Default constructor for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

GaussianControlPointList::GaussianControlPointList() :
    AttributeSubject(GaussianControlPointList::TypeMapFormatString)
{
    GaussianControlPointList::Init();
}

// ****************************************************************************
// Method: GaussianControlPointList::GaussianControlPointList
//
// Purpose:
//   Constructor for the derived classes of GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

GaussianControlPointList::GaussianControlPointList(private_tmfs_t tmfs) :
    AttributeSubject(tmfs.tmfs)
{
    GaussianControlPointList::Init();
}

// ****************************************************************************
// Method: GaussianControlPointList::GaussianControlPointList
//
// Purpose:
//   Copy constructor for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

GaussianControlPointList::GaussianControlPointList(const GaussianControlPointList &obj) :
    AttributeSubject(GaussianControlPointList::TypeMapFormatString)
{
    GaussianControlPointList::Copy(obj);
}

// ****************************************************************************
// Method: GaussianControlPointList::GaussianControlPointList
//
// Purpose:
//   Copy constructor for derived classes of the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

GaussianControlPointList::GaussianControlPointList(const GaussianControlPointList &obj, private_tmfs_t tmfs) :
    AttributeSubject(tmfs.tmfs)
{
    GaussianControlPointList::Copy(obj);
}

// ****************************************************************************
// Method: GaussianControlPointList::~GaussianControlPointList
//
// Purpose:
//   Destructor for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

GaussianControlPointList::~GaussianControlPointList()
{
    AttributeGroupVector::iterator pos;

    // Destroy the controlPoints field.
    for(pos = controlPoints.begin(); pos != controlPoints.end(); ++pos)
        delete *pos;
}

// ****************************************************************************
// Method: GaussianControlPointList::operator =
//
// Purpose:
//   Assignment operator for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

GaussianControlPointList&
GaussianControlPointList::operator = (const GaussianControlPointList &obj)
{
    if (this == &obj) return *this;

    GaussianControlPointList::Copy(obj);

    return *this;
}

// ****************************************************************************
// Method: GaussianControlPointList::operator ==
//
// Purpose:
//   Comparison operator == for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

bool
GaussianControlPointList::operator == (const GaussianControlPointList &obj) const
{
    bool controlPoints_equal = (obj.controlPoints.size() == controlPoints.size());
    for(size_t i = 0; (i < controlPoints.size()) && controlPoints_equal; ++i)
    {
        // Make references to GaussianControlPoint from AttributeGroup *.
        const GaussianControlPoint &controlPoints1 = *((const GaussianControlPoint *)(controlPoints[i]));
        const GaussianControlPoint &controlPoints2 = *((const GaussianControlPoint *)(obj.controlPoints[i]));
        controlPoints_equal = (controlPoints1 == controlPoints2);
    }

    // Create the return value
    return (controlPoints_equal);
}

// ****************************************************************************
// Method: GaussianControlPointList::operator !=
//
// Purpose:
//   Comparison operator != for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

bool
GaussianControlPointList::operator != (const GaussianControlPointList &obj) const
{
    return !(this->operator == (obj));
}

// ****************************************************************************
// Method: GaussianControlPointList::TypeName
//
// Purpose:
//   Type name method for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

const std::string
GaussianControlPointList::TypeName() const
{
    return "GaussianControlPointList";
}

// ****************************************************************************
// Method: GaussianControlPointList::CopyAttributes
//
// Purpose:
//   CopyAttributes method for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

bool
GaussianControlPointList::CopyAttributes(const AttributeGroup *atts)
{
    if(TypeName() != atts->TypeName())
        return false;

    // Call assignment operator.
    const GaussianControlPointList *tmp = (const GaussianControlPointList *)atts;
    *this = *tmp;

    return true;
}

// ****************************************************************************
// Method: GaussianControlPointList::CreateCompatible
//
// Purpose:
//   CreateCompatible method for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

AttributeSubject *
GaussianControlPointList::CreateCompatible(const std::string &tname) const
{
    AttributeSubject *retval = 0;
    if(TypeName() == tname)
        retval = new GaussianControlPointList(*this);
    // Other cases could go here too.

    return retval;
}

// ****************************************************************************
// Method: GaussianControlPointList::NewInstance
//
// Purpose:
//   NewInstance method for the GaussianControlPointList class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

AttributeSubject *
GaussianControlPointList::NewInstance(bool copy) const
{
    AttributeSubject *retval = 0;
    if(copy)
        retval = new GaussianControlPointList(*this);
    else
        retval = new GaussianControlPointList;

    return retval;
}

// ****************************************************************************
// Method: GaussianControlPointList::SelectAll
//
// Purpose:
//   Selects all attributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void
GaussianControlPointList::SelectAll()
{
    Select(ID_controlPoints, (void *)&controlPoints);
}

// ****************************************************************************
// Method: GaussianControlPointList::CreateSubAttributeGroup
//
// Purpose:
//   This class contains a list of GaussianControlPoint objects.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

AttributeGroup *
GaussianControlPointList::CreateSubAttributeGroup(int)
{
    return new GaussianControlPoint;
}

///////////////////////////////////////////////////////////////////////////////
// Persistence methods
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: GaussianControlPointList::CreateNode
//
// Purpose:
//   This method creates a DataNode representation of the object so it can be saved to a config file.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

bool
GaussianControlPointList::CreateNode(DataNode *parentNode, bool completeSave, bool forceAdd)
{
    if(parentNode == 0)
        return false;

    GaussianControlPointList defaultObject;
    bool addToParent = false;
    // Create a node for GaussianControlPointList.
    DataNode *node = new DataNode("GaussianControlPointList");

    if(completeSave || !FieldsEqual(ID_controlPoints, &defaultObject))
    {
        addToParent = true;
        for(size_t i = 0; i < controlPoints.size(); ++i)
            controlPoints[i]->CreateNode(node, completeSave, true);
    }


    // Add the node to the parent node.
    if(addToParent || forceAdd)
        parentNode->AddNode(node);
    else
        delete node;

    return (addToParent || forceAdd);
}

// ****************************************************************************
// Method: GaussianControlPointList::SetFromNode
//
// Purpose:
//   This method sets attributes in this object from values in a DataNode representation of the object.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void
GaussianControlPointList::SetFromNode(DataNode *parentNode)
{
    if(parentNode == 0)
        return;

    DataNode *searchNode = parentNode->GetNode("GaussianControlPointList");
    if(searchNode == 0)
        return;

    DataNode **children;

    // Clear all the GaussianControlPoints if we got any.
    bool clearedControlPoints = false;
    // Go through all of the children and construct a new
    // GaussianControlPoint for each one of them.
    children = searchNode->GetChildren();
    if(children != 0)
    {
        for(int i = 0; i < searchNode->GetNumChildren(); ++i)
        {
            if(children[i]->GetKey() == std::string("GaussianControlPoint"))
            {
                if (!clearedControlPoints)
                {
                    ClearControlPoints();
                    clearedControlPoints = true;
                }
                GaussianControlPoint temp;
                temp.SetFromNode(children[i]);
                AddControlPoints(temp);
            }
        }
    }

}

///////////////////////////////////////////////////////////////////////////////
// Set property methods
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Get property methods
///////////////////////////////////////////////////////////////////////////////

const AttributeGroupVector &
GaussianControlPointList::GetControlPoints() const
{
    return controlPoints;
}

AttributeGroupVector &
GaussianControlPointList::GetControlPoints()
{
    return controlPoints;
}

///////////////////////////////////////////////////////////////////////////////
// Select property methods
///////////////////////////////////////////////////////////////////////////////

void
GaussianControlPointList::SelectControlPoints()
{
    Select(ID_controlPoints, (void *)&controlPoints);
}

///////////////////////////////////////////////////////////////////////////////
// AttributeGroupVector convenience methods.
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: GaussianControlPointList::AddControlPoints
//
// Purpose:
//   This class contains a list of GaussianControlPoint objects.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void
GaussianControlPointList::AddControlPoints(const GaussianControlPoint &obj)
{
    GaussianControlPoint *newGaussianControlPoint = new GaussianControlPoint(obj);
    controlPoints.push_back(newGaussianControlPoint);

    // Indicate that things have changed by selecting it.
    Select(ID_controlPoints, (void *)&controlPoints);
}

// ****************************************************************************
// Method: GaussianControlPointList::ClearControlPoints
//
// Purpose:
//   This class contains a list of GaussianControlPoint objects.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void
GaussianControlPointList::ClearControlPoints()
{
    AttributeGroupVector::iterator pos;

    for(pos = controlPoints.begin(); pos != controlPoints.end(); ++pos)
        delete *pos;
    controlPoints.clear();

    // Indicate that things have changed by selecting the list.
    Select(ID_controlPoints, (void *)&controlPoints);
}

// ****************************************************************************
// Method: GaussianControlPointList::RemoveControlPoints
//
// Purpose:
//   This class contains a list of GaussianControlPoint objects.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void
GaussianControlPointList::RemoveControlPoints(int index)
{
    AttributeGroupVector::iterator pos = controlPoints.begin();

    // Iterate through the vector "index" times.
    for(int i = 0; i < index; ++i)
        if(pos != controlPoints.end()) ++pos;

    // If pos is still a valid iterator, remove that element.
    if(pos != controlPoints.end())
    {
        delete *pos;
        controlPoints.erase(pos);
    }

    // Indicate that things have changed by selecting the list.
    Select(ID_controlPoints, (void *)&controlPoints);
}

// ****************************************************************************
// Method: GaussianControlPointList::GetNumControlPoints
//
// Purpose:
//   This class contains a list of GaussianControlPoint objects.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

int
GaussianControlPointList::GetNumControlPoints() const
{
    return (int)controlPoints.size();
}

// ****************************************************************************
// Method: GaussianControlPointList::GetControlPoints
//
// Purpose:
//   This class contains a list of GaussianControlPoint objects.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

GaussianControlPoint &
GaussianControlPointList::GetControlPoints(int i)
{
    return *((GaussianControlPoint *)controlPoints[i]);
}

// ****************************************************************************
// Method: GaussianControlPointList::GetControlPoints
//
// Purpose:
//   This class contains a list of GaussianControlPoint objects.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

const GaussianControlPoint &
GaussianControlPointList::GetControlPoints(int i) const
{
    return *((GaussianControlPoint *)controlPoints[i]);
}

// ****************************************************************************
// Method: GaussianControlPointList::operator []
//
// Purpose:
//   This class contains a list of GaussianControlPoint objects.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

GaussianControlPoint &
GaussianControlPointList::operator [] (int i)
{
    return *((GaussianControlPoint *)controlPoints[i]);
}

// ****************************************************************************
// Method: GaussianControlPointList::operator []
//
// Purpose:
//   This class contains a list of GaussianControlPoint objects.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

const GaussianControlPoint &
GaussianControlPointList::operator [] (int i) const
{
    return *((GaussianControlPoint *)controlPoints[i]);
}

///////////////////////////////////////////////////////////////////////////////
// Keyframing methods
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: GaussianControlPointList::GetFieldName
//
// Purpose:
//   This method returns the name of a field given its index.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

std::string
GaussianControlPointList::GetFieldName(int index) const
{
    switch (index)
    {
    case ID_controlPoints: return "controlPoints";
    default:  return "invalid index";
    }
}

// ****************************************************************************
// Method: GaussianControlPointList::GetFieldType
//
// Purpose:
//   This method returns the type of a field given its index.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

AttributeGroup::FieldType
GaussianControlPointList::GetFieldType(int index) const
{
    switch (index)
    {
    case ID_controlPoints: return FieldType_attVector;
    default:  return FieldType_unknown;
    }
}

// ****************************************************************************
// Method: GaussianControlPointList::GetFieldTypeName
//
// Purpose:
//   This method returns the name of a field type given its index.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

std::string
GaussianControlPointList::GetFieldTypeName(int index) const
{
    switch (index)
    {
    case ID_controlPoints: return "attVector";
    default:  return "invalid index";
    }
}

// ****************************************************************************
// Method: GaussianControlPointList::FieldsEqual
//
// Purpose:
//   This method compares two fields and return true if they are equal.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

bool
GaussianControlPointList::FieldsEqual(int index_, const AttributeGroup *rhs) const
{
    const GaussianControlPointList &obj = *((const GaussianControlPointList*)rhs);
    bool retval = false;
    switch (index_)
    {
    case ID_controlPoints:
        {  // new scope
        bool controlPoints_equal = (obj.controlPoints.size() == controlPoints.size());
        for(size_t i = 0; (i < controlPoints.size()) && controlPoints_equal; ++i)
        {
            // Make references to GaussianControlPoint from AttributeGroup *.
            const GaussianControlPoint &controlPoints1 = *((const GaussianControlPoint *)(controlPoints[i]));
            const GaussianControlPoint &controlPoints2 = *((const GaussianControlPoint *)(obj.controlPoints[i]));
            controlPoints_equal = (controlPoints1 == controlPoints2);
        }

        retval = controlPoints_equal;
        }
        break;
    default: retval = false;
    }

    return retval;
}

///////////////////////////////////////////////////////////////////////////////
// User-defined methods.
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: GaussianControlPointList::SetNumControlPoints
//
// Purpose:
//   Resized the controlpoints vector, adding or removing points as needed. 
//
// Note:       Generated by xml2atts from code file.
//
// Programmer: Kathleen Biagas 
// Creation:   May 24, 2022
//
// Modifications:
//
// ****************************************************************************

void
GaussianControlPointList::SetNumControlPoints(const int n)
{
    int currentSize = GetNumControlPoints();

    if(n == currentSize)
    {
        return;
    }
    else if (n == 0)
    {
        // ClearControlPoints calls Select so there is no need to progress to
        // the bottom of this method to repeat Select.
        ClearControlPoints();
        return;
    }
    else if(n < currentSize)
    {
        AttributeGroupVector::iterator pos = controlPoints.end();
        pos--;
        for (int i = 0; i < (currentSize-n); ++i, --pos)
        {
            delete *pos;
            controlPoints.erase(pos);
        }
    }
    else
    {
        float delta = 1.f/float(n-1);
        float pos = 0.f;
        for(int i = 0; i < currentSize; ++i, pos+=delta)
        {
            ((GaussianControlPoint*)(controlPoints[i]))->SetX(pos);
        }
        controlPoints.resize(n);
        for(int i = currentSize; i < n; ++i, pos+=delta)
        {
            GaussianControlPoint *gpt = new GaussianControlPoint();
            gpt->SetX(pos);
            gpt->SetHeight(0.5f);
            gpt->SetWidth(0.03f);
            gpt->SetXBias(0.0f);
            gpt->SetYBias(0.0f);
            controlPoints[i] = gpt;
        }
    }

    Select(ID_controlPoints, (void*)&controlPoints);
}

